package DFS;

import java.util.ArrayList;

/**
 * @Author: dust
 * @Date: 2019/11/18 16:27
 */
public class GraphDFS {

    private Graph G;
    private boolean[] visited;
    private ArrayList<Integer> pre=new ArrayList<>();
    private ArrayList<Integer> post=new ArrayList<>();

    public GraphDFS(Graph G) {

        this.G=G;
        visited=new boolean[G.V()];

       for(int v=0;v<G.V();v++){
           if(!visited[v]){
               dfs(v);
           }
       }
    }

    private void dfs(int v){
        visited[v]=true;
        pre.add(v);
        for(int w:G.adj(v)){
            if(!visited[w]){
                dfs(w);
            }
        }
        post.add(v);

    }

    public Iterable<Integer>pre(){
        return pre;
    }

    public Iterable<Integer>post(){
        return post;
    }

    public static void main(String[] args) {
        Graph g=new Graph("g.txt");
        GraphDFS dfs=new GraphDFS(g);
        System.out.println(dfs.pre());
    }

}
